<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>考试功能演示</title>
<link rel="stylesheet" href="http://cdn.amazeui.org/amazeui/2.7.2/css/amazeui.min.css" />

</head>
<body>
<div class="am-container dataDiv" id="data">
			<section class="am-panel am-panel-primary">
			  <header class="am-panel-hd">
			    <h3 class="am-panel-title">考试功能演示</h3>
			  </header>
			  <div class="am-panel-bd">
					<table class="am-table am-table-bordered am-text-center">
						<thead>
							<tr>
								<th colspan="3">
									<button title="点击此按钮刷新下方表格数据，ajax处理" class="am-btn am-btn-primary" onclick="refresh();">刷新数据</button>
								</th>
							</tr>
							
						</thead>
						<tbody>
					     <c:forEach items="${results}" var="c" varStatus="index">
					     <tr>
					     <td><input type="checkbox" name="checked"></td>
					     <td>${c.name}</td>
					     <td>${c.age}</td>
					     </tr>
					     </c:forEach>
							<tr>
								
								<td colspan="3">
									<button class="am-btn am-btn-primary" title="点击后将所有数据的checkbox修改为选中状态" onclick="checkAll();">全选</button>
									<button class="am-btn am-btn-primary" title="点击后将所有数据的checkbox修改为未选中状态" onclick="notCheckAll();">全不选</button>
									<button class="am-btn am-btn-primary" title="点击后，已选择的checkbox改为未选择，未选择的checkbox改为已选择" onclick="checkInverse();">反选</button>
									<button class="am-btn am-btn-warning" title="点击后提交已选择数据的value值" onclick="submitData();">提交已选择数据</button>
								</td>
							</tr>
						</tbody>
					</table>
					<div>
						<p id="message">点击提交按钮后，如果未选择任何数据，在这里打印提示信息，提示用户先选择数据再提交</p>
						
					</div>
			  </div>
			</section>
		</div>
		<script type="text/javascript" src="http://localhost:8080/test4/static/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
	function refresh(){
		$('#dataTable').load("http://localhost:8080/test4/ajax.do",function(data){
			$('#dataTable').html('data');
		});
	}
	function checkAll(){
		 var td = $("td > input");
		console.log(td); 
		/* var td = document.getElementByName('checked');
		console.log(td); */
		td.each(function(){
			console.log(this.checked);
			/* console.log($(this).checked); */
			if(this.checked){
				
			}else{
				$(this).attr("checked","checked");
			}
		});
	}
	function notCheckAll(){
		 var td = $("td > input");
		console.log(td); 
		td.each(function(){
			console.log(this.checked);
			
			if(this.checked){
				$(this).removeAttr("checked","checked");
			}else{
				
			}
		});
	}
	
	function checkInverse() {
		var td = $("td > input");
		console.log(td);
		td.each(function() {

			if (this.checked) {
				console.log("sdadasdasd");
				$(this).removeAttr("checked", "checked");
				console.log($(this).siblings());
				$(this).siblings().attr("checked", "checked");/*同级元素添加checked  */
			}
		});
	}
	function submitData(){
		var count=0;
		var td = $("td > input");
		td.each(function() {
		if (this.checked) {
				count++;
			}
		if(count==0){
			$('#message').text("未选择任何数据");
		}
		if(count>0){
			if(this.checked){
				var date = $(this).siblings().text();
				console.log(data);
			}
			
		}
		});
	}
</script>
</body>
</html>